package com.bianguo.android.apushdemo.view.wiget;

import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ScrollView;

import com.bianguo.android.apushdemo.R;
import com.bianguo.android.apushdemo.utils.MyLog;

/**
 * Created by Administrator on 2017/8/31.
 */

public class ZoomScrollView extends ScrollView {

    private float lastY;
    private View zoomView;
    private int zoomViewHeight, zoomViewWidth;
    private int zoomId;
    private int zoomHeight;
    private boolean isDowning, isFirstTouch;
    private OnScrollChangedListener onScrollChangedListener;

    public void addOnScrollChangedListener(OnScrollChangedListener onScrollChangedListener) {
        this.onScrollChangedListener = onScrollChangedListener;
    }

    public ZoomScrollView(Context context) {
        super(context);
        initView(context, null);
    }

    public ZoomScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context, attrs);
    }

    public ZoomScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context, attrs);
    }

    private void initView(Context mContext, AttributeSet attrs) {
        TypedArray t = mContext.obtainStyledAttributes(attrs, R.styleable.ZoomScrollView);
        zoomId = t.getResourceId(R.styleable.ZoomScrollView_zoomId, -1);
        zoomHeight = (int) t.getDimension(R.styleable.ZoomScrollView_zoomHeight, 0);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        setOverScrollMode(OVER_SCROLL_NEVER);
//        zoomView = ((ViewGroup) getChildAt(0)).getChildAt(0);
        zoomView = findViewById(zoomId);
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if (zoomView == null) {
            return super.dispatchTouchEvent(ev);
        }
        float y = ev.getY();
        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                if (zoomView != null && !isFirstTouch) {
                    zoomViewWidth = zoomView.getMeasuredWidth();
                    zoomViewHeight = zoomView.getMeasuredHeight();
                    isFirstTouch = true;
                }
                break;
            case MotionEvent.ACTION_MOVE:
                if (!isDowning) {
                    if (getScrollY() == 0) {
                        lastY = ev.getY();//滑动到顶部时，记录位置
                    } else {
                        break;
                    }
                    isDowning = true;
                }
                float scrollY = y - lastY;
                if (scrollY > 0) {
                    ViewGroup.LayoutParams layoutParams = zoomView.getLayoutParams();
                    layoutParams.height = (int) (zoomViewHeight + scrollY / 2);
                    zoomView.setLayoutParams(layoutParams);
                    if (zoomView.getMeasuredHeight() > zoomViewHeight) {
                        return true;
                    }
                }
                break;
            case MotionEvent.ACTION_UP:
                isDowning = false;
                replyView();
                break;
        }
        return super.dispatchTouchEvent(ev);
    }

    /**回弹*/
    private void replyView() {
        final float distance = zoomView.getMeasuredHeight() - zoomViewHeight;
        // 设置动画
        ValueAnimator anim = ObjectAnimator.ofFloat(distance, 0.0F).setDuration((long) (distance * 0.2f));
        anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                setZoom((Float) animation.getAnimatedValue());
            }
        });
        anim.start();
    }

    /**放大view*/
    private void setZoom(float s) {
        float scaleTimes = (float) ((zoomViewWidth+s)/(zoomViewWidth*1.0));
//        如超过最大放大倍数，直接返回
        if (scaleTimes > 2f) return;

        ViewGroup.LayoutParams layoutParams = zoomView.getLayoutParams();
        layoutParams.width = (int) (zoomViewWidth + s);
        layoutParams.height = (int)(zoomViewHeight*((zoomViewWidth+s)/zoomViewWidth));
//        设置控件水平居中
        ((MarginLayoutParams) layoutParams).setMargins(-(layoutParams.width - zoomViewWidth) / 2, 0, 0, 0);
        zoomView.setLayoutParams(layoutParams);
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        return super.onTouchEvent(ev);
    }

    @Override
    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
        super.onScrollChanged(l, t, oldl, oldt);
        if (onScrollChangedListener != null) {
            onScrollChangedListener.onScrollChanged(l, t, oldl, oldt);
        }
    }

    public interface OnScrollChangedListener {
        void onScrollChanged(int x, int y, int oldX, int oldY);
    }
}
